#include <ctype.h>
#include <stdio.h>
#include <string.h>

//串的实现
//定义串的最大容量
#define MAX_SIZE 100
//定义串的类型
typedef struct {
    char ch[MAX_SIZE];
    int len;//串的实际长度
}SeqString;

//给串赋值
void strAssign(SeqString *t,char *chars) {
    if (chars == NULL || t ==NULL) {
        printf("参数错误");
        return;
    }
    int i = 0;
    while (chars[i] != '\0') {
        t->ch[i] = chars[i];
        i++;
    }
    t->len = i;
    // printf("%s",t->ch);
}
//输出串的长度
int strLen(SeqString *t) {
    if (t == NULL ||t->len <= 0) {
        return 0;
    }
    return t->len;
}
//复制串
void strCopy(SeqString *des,SeqString *src) {
    if (des == NULL || src == NULL) {
        return ;
    }
    if(src->len<0 || src->len>MAX_SIZE) {
        return ;
    }
    des->len = 0;
    for (int i = 0; i < src->len; i++) {
        des->ch[i] = src->ch[i];
    }
    des->len = src->len;
    des->ch[src->len]='\0';
}

//strEqual() 判断两个串是否相等
int strEqual(SeqString *S,SeqString *T){
    for (int i = 0; i <S->len&&S->len; i++) {
        if(S->ch[i]!=T->ch[i]){
            return 0;
        }
    }
    printf("========\n");
    return 1;
}

//strConnect()连接两个串
void strConnect(SeqString *s,SeqString *t){
    if(s->len+t->len>MAX_SIZE){
        printf("拼接后串超出范围");
        return;
    }
    for (int i = 0; i < t->len; i++) {
        s->ch[s->len+i]=t->ch[i];
    }
    s->len+=t->len;
    s->ch[s->len]='\0';
    printf("result : %s\n", s->ch);
}

//strCompete() 比较两个串的大小  按照编码来比较，如果s>t 返回1 ； s=t 返回0 ；s <t 返回-1
int strCompete(SeqString *s,SeqString *t){
    if(s->len > t->len){
        printf("1");
        return 1;
    }
    if(s->len<t->len){
        printf("-1");
        return -1;
    }
    if(s->len==t->len){
        for(int i = 0;i<s->len;++i){
            if(s->ch[i]>t->ch[i]){
                printf("1");
                return 1;
            }
            if(s->ch[i]<t->ch[i]){
                printf("-1");
                return -1;
            }
        }
        printf("0");
        return  0;
    }
    return 0;
}

//Insert () 插入操作
void Insert(SeqString *s,int pos,SeqString *t){
    if(pos<1 || pos>s->len){
        printf("插入位置不合法");
    }
    if(s->len+t->len>MAX_SIZE){
        printf("插入后，超过最大容量");
    }
    for (int i = s->len -1; i >=pos ; i--) {
        s->ch[i+t->len]=s->ch[i];
    }
    for (int i = 0; i < t->len; ++i) {
        s->ch[pos-1+i]=t->ch[i];
    }
    s->len+=t->len;
    printf("\nresult : %s \n", s->ch);
}

int main(void) {
    SeqString s;
    printf("test message\n");
    strAssign(&s,"abc");
    printf("%d\n",strLen(&s));
    strEqual(&s,&s);
    strConnect(&s,&s);
    strCompete(&s,&s);
    Insert(&s,3,&s);
    return 0;
}